package com.atgugui.vod.service.impl;

import com.aliyun.oss.model.UploadFileRequest;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atgugui.vod.service.VodService;
import com.atgugui.vod.utils.InitVodObject;
import com.atgugui.vod.utils.VodConstantPropertiesUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

@SuppressWarnings("all")
@Service
public class VodServiceImpl implements VodService {

    @Override
    public String uploadVideoAly(MultipartFile file) {
        try {
            String keyId = VodConstantPropertiesUtils.KEY_ID;
            String keySecret = VodConstantPropertiesUtils.KEY_SECRET;
            String filename = file.getOriginalFilename(); // 文件名
            String title = filename.substring(0,filename.lastIndexOf(".")); // 去掉视频类型后缀名
            InputStream inputStream = file.getInputStream();
            UploadStreamRequest request = new UploadStreamRequest(keyId, keySecret, title, filename, inputStream);
            UploadVideoImpl uploadVideo = new UploadVideoImpl();
            UploadStreamResponse response = uploadVideo.uploadStream(request);
            String videoId = null;
            if (response.isSuccess()) {
                videoId = response.getVideoId();
            } else {
                videoId = response.getVideoId();
            }
            return videoId;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean deleteVideoByVideoId(String videoId) {
        try {
            String keySecret = VodConstantPropertiesUtils.KEY_SECRET;
            String keyId = VodConstantPropertiesUtils.KEY_ID;
            DefaultAcsClient client = InitVodObject.initVodClient(keyId, keySecret);
            // 创建删除视频对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            // 设置被删除的视频对象的id
            request.setVideoIds(videoId);
            // 执行删除
            client.getAcsResponse(request);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public void deleteBatch(List<String> videoIdList) {
        try {
            String keySecret = VodConstantPropertiesUtils.KEY_SECRET;
            String keyId = VodConstantPropertiesUtils.KEY_ID;
            DefaultAcsClient client = InitVodObject.initVodClient(keyId, keySecret);
            // 创建删除视频对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            // 设置被删除的视频对象的id
            String videoIdsStr = StringUtils.join(videoIdList.toArray(),',');
            request.setVideoIds(videoIdsStr);
            // 执行删除
            client.getAcsResponse(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getAuthPlayByVideoId(String videoId) {
        // 获取vod相关参数
        try {
            String keyId = VodConstantPropertiesUtils.KEY_ID;
            String keySecret = VodConstantPropertiesUtils.KEY_SECRET;

            // 初始化一个Vod的Client对象
            DefaultAcsClient vodClient = InitVodObject.initVodClient(keyId,keySecret);

            // 请求授权，通过resquest对象请求(请求授权对象),通过视频唯一id授权
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(videoId);

            // 响应授权
            GetVideoPlayAuthResponse response = vodClient.getAcsResponse(request);

            // 获取授权码
            String playAuth = response.getPlayAuth();
            return playAuth;
        } catch (Exception e) {
            e.printStackTrace();
            return "授权失败";
        }
    }
}
